package com.codechuan.didi_hack;

import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;

import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.codechuan.didi_hack.utils.ContextUtils;


public class FloatWindowManager {
    private final String TAG = "FloatWindowManager";
    private final WindowManager windowManager;

    private static FloatWindowManager instance = new FloatWindowManager();
    private final LayoutInflater inflater;

    private View toolView;
    private View logView;
    private View handlerView;

    public static FloatWindowManager get() {
        return instance;
    }


    private FloatWindowManager() {
        inflater = LayoutInflater.from(ContextUtils.application);
        windowManager = (WindowManager) ContextUtils.application.getSystemService(Context.WINDOW_SERVICE);
    }

    public void showToolView() {
        if (toolView != null) {
            return;
        }
        toolView = inflater.inflate(R.layout.window_tool_view, null, false);
        toolView.findViewById(R.id.tvFunction).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hindAllView();
                showHandlerView();
            }
        });
     /*   toolView.findViewById(R.id.tvFunction1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(TAG, "点击了功能一");
            }
        });*/
        // 日志
        TextView tvFunction2 = toolView.findViewById(R.id.tvFunction2);
        tvFunction2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(TAG, "点击了功能二");
                if (logView == null) {
                    showLogView();
                    tvFunction2.setText("隐藏日志");
                } else {
                    hindLogView();
                    tvFunction2.setText("显示日志");
                }
            }
        });
      /*  toolView.findViewById(R.id.tvFunction3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(TAG, "点击了功能三");
            }
        });*/
        toolView.findViewById(R.id.tvFunction4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(TAG, "点击了功能四");
            }
        });
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        layoutParams.format = PixelFormat.TRANSLUCENT;
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        layoutParams.gravity = Gravity.START | Gravity.BOTTOM;
        windowManager.addView(toolView, layoutParams);
    }



    public void showHandlerView() {
        if (handlerView != null) {
            return;
        }
        handlerView = inflater.inflate(R.layout.float_window_handler_view, null, false);
        handlerView.findViewById(R.id.tvHandler).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    hindHandlerView();
                    showToolView();
            }
        });
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        layoutParams.format = PixelFormat.TRANSLUCENT;
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        layoutParams.gravity = Gravity.START | Gravity.CENTER_VERTICAL;
        windowManager.addView(handlerView, layoutParams);
    }


    public void showLogView() {
        if (logView != null) {
            return;
        }
        logView = inflater.inflate(R.layout.float_window_log_view, null, false);
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        layoutParams.format = PixelFormat.TRANSLUCENT;
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;;
        layoutParams.y = 100;
        layoutParams.gravity = Gravity.START | Gravity.TOP;
        windowManager.addView(logView, layoutParams);
    }

    public void hindHandlerView(){
        if (handlerView != null) {
            windowManager.removeView(handlerView);
            handlerView = null;
        }
    }

    public void hindToolView() {
        if (toolView != null) {
            windowManager.removeView(toolView);
            toolView = null;
        }
    }

    public void hindLogView() {
        if (logView != null) {
            windowManager.removeView(logView);
            logView = null;
        }
    }

    public void hindAllView() {
        if (toolView != null) {
            windowManager.removeView(toolView);
            toolView = null;
        }
    }

    // 日志代码
    public void addLog(String log){
        Message message = handler.obtainMessage();
        message.obj = log;
        handler.sendMessage(message);
    }

    private int maxLine = 10;
    private int line =  0;
    private Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (logView == null) {
                return;
            }
            if(msg.obj==null){
                return;
            }
            line++;
            TextView tvLog = logView.findViewById(R.id.tvLog);
            tvLog.append(msg.obj.toString()+"\n");
            if (line > maxLine) {
                tvLog.setText("");
                line = 0;
            }
        }
    };


//    /***********************************************************todo,测试代码********************************/
//
//    @SuppressLint("HandlerLeak")
//    private void test() {
//        if (handler != null) {
//            handler.removeMessages(0);
//        }
//        handler = new Handler() {
//            int line = 0;
//            @Override
//            public void handleMessage(@NonNull Message msg) {
//                super.handleMessage(msg);
//
//            }
//        };
//    }
    /***********************************************************todo,测试代码********************************/
}
